<?php

namespace App\Jobs;

use App\Services\Admin\User;
use App\Services\Admin\UserSubscribeService;
use App\Services\Api\EasyWeChatService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\UserSubscribe;
use App\Models\ClassBaseInfo;
use App\Services\Api\SubscribeServices;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;

class CourseReminder implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $order;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(UserSubscribe $order,$delay,$id)
    {
        $this->order = $order->where('id',$id)->first();
        // 设置延迟的时间，delay() 方法的参数代表多少秒之后执行
        $this->delay($delay);
        Log::info('提醒加入成功-'.'id-'.$id.'秒-'.$delay);
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if($this->order->subscribe_status == 2) {
            $user_info = User::info($this->order->user_id);

            $data = [
                'character_string1' => ['value' => $user_info->user_number],            //会员编号
                'thing2' => ['value' => $this->order->class_name],                      //課程名稱
                'thing3' => ['value' => $this->order->store_name],                      //上课地点
                'time4' => ['value' => $this->order->class_time],                       //日期
                'thing5' => ['value' => '預約成功'],                                      //預約狀態
            ];

            EasyWeChatService::sendUserMessage($this->order->user_id, $data);
        }
    }
}
